﻿using Fleck;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Management;
using System.Printing;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TemplatesPrint.Tools
{
    public class GetServerFileTool
    {
        static bool ConnectState(string path, string userName, string passWord,out string exinfo)
        {
            exinfo = "";
            int tryNum = 0;
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string clearConnect = $"net use {path} /del";
                proc.StandardInput.WriteLine(clearConnect);

                Thread.Sleep(10);

                string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";

                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited&& tryNum<10)
                {
                    proc.WaitForExit(1000);
                    tryNum++;
                }
                if (tryNum < 10)
                {
                    string errormsg = proc.StandardError.ReadToEnd();
                    proc.StandardError.Close();
                    if (string.IsNullOrEmpty(errormsg))
                    {
                        Flag = true;
                    }
                    else
                    {
                        exinfo=errormsg;
                    }
                }
                else
                {
                    exinfo="连接服务器失败";
                }
              
            }
            catch (Exception ex)
            {
                //throw ex;
                exinfo = ex.Message;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

        //public static void CopyTemplate(string sourceFile, string destFile)
        //{

        //    var status = ConnectState(@"\\" + Common.SharePath + @"\" + Common.SharePublished, Common.ShareUser, Common.SharePassWord);
        //    if (status)
        //    {
        //        DirectoryInfo directory = new DirectoryInfo(@"\\" + Common.SharePath + @"\" + Common.SharePublished);
        //        FileSystemInfo[] filesArray = directory.GetFileSystemInfos().OrderBy(x => x.CreationTime).ToArray();

        //        File.Copy(sourceFile, destFile);
        //    }
        //    else
        //    {
        //        //无法访问共享文件夹
        //    }

        //}

        public static string GetTemplatePath(string path,string directory,string file, string username,string pw ,out string ex)
        {
            var status = ConnectState(@"\\" + path + @"\" + directory, username, pw, out ex);
            if (status)
            {
                return @"\\" + path + @"\" + directory + @"\" + file + ".frx";
            }
            else
            {
                return null;
            }
        }

        //public static List<FileSystemInfo> GetTemplateList(string filePath = null)
        //{
        //    try
        //    {
        //        //var path = @"\\" + Common.SharePath + @"\" + Common.SharePublished;
        //        if (filePath == null)
        //        {
        //            filePath = path;
        //        }
        //        var ret = new List<FileSystemInfo>();
        //        var status = ConnectState(filePath, Common.ShareUser, Common.SharePassWord);
        //        if (status)
        //        {
        //            DirectoryInfo directory = new DirectoryInfo(filePath);
        //            FileSystemInfo[] filesArray = directory.GetFileSystemInfos().OrderBy(x => x.CreationTime).ToArray();
        //            foreach (var item in filesArray)
        //            {
        //                //是否是一个文件夹
        //                if (item.Attributes != FileAttributes.Directory)
        //                {
        //                    ret.Add(item);
        //                }
        //            }
        //        }
        //        else
        //        {
        //            //无法访问共享文件夹
        //        }
        //        return ret;
        //    }
        //    catch(Exception ex)
        //    {
        //        return null;
        //    }
        //}
    }
}



